package com.houdoudou.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.houdoudou.bean.TelnumVerify;

public class TelnumVerifyDao {

	 Connection conn=null;	
	public TelnumVerifyDao(Connection conn) {
		super();
		this.conn = conn;
	}

	public  long create( TelnumVerify telnumVerify ) throws SQLException{	
		long result=0;
		  PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 	
		   String sql=null;
	
	        try {
	        	sql = "insert  into telnumverify (telnum,code,validTime ) " //9个参数
	        + "values (?,?,?)";
	        	//共45个参数。
	        	pstm=conn.prepareStatement(sql);
	        	pstm.setString(1, telnumVerify.getTelnum());
	        	pstm.setString(2, telnumVerify.getCode());
	        	pstm.setString(3, telnumVerify.getValidTime());
	        	
				pstm.executeUpdate(); 	
			} finally{
				ConnFactory.closeStatment(pstm, pstm, rs);
			}			
		return result;
	}
	
	
	 public boolean check(TelnumVerify telnumVerify) throws Exception{
		  PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 	
		  boolean result=false;
	        try {   
	        	 String sql="select * from telnumverify  where telnum = "+ telnumVerify.getTelnum()+" and code="+ telnumVerify.getCode()+ " and validTime >"+telnumVerify.getValidTime();	  	       
				stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
				rs=stm.executeQuery(sql);		
		        while(rs.next()){		        
		           result=true;
		        }
		        sql= "delete from telnumverify where validTime < "+telnumVerify.getValidTime();  
		        stm.executeUpdate(sql);
		        if(result){
		        sql= "delete from telnumverify  where telnum = "+ telnumVerify.getTelnum()+" and code="+ telnumVerify.getCode();  
		        stm.executeUpdate(sql); }
		        
	        }finally{
				ConnFactory.closeStatment(pstm, pstm, rs);
			}
	        
		   return result;
	   }
}
